package com.bjtu.douyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjtu.common.pojo.Comment;
import com.bjtu.common.pojo.CommentLike;
import com.bjtu.common.pojo.Page;
import com.bjtu.common.util.Result;
import com.bjtu.douyin.mapper.CommentLikeMapper;
import com.bjtu.douyin.mapper.CommentMapper;
import com.bjtu.douyin.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {
    private final CommentMapper commentMapper;

    private final CommentLikeMapper commentLikeMapper;

    @Override
    public Page page(Integer page, Integer pageSize, Integer videoId, Integer userId) {
        PageHelper.startPage(page, pageSize);
        List<Comment> commentList = commentMapper.selectComments(videoId);

        // 设置每个评论的 isLiked 字段
        for (Comment comment : commentList) {
            QueryWrapper<CommentLike> likeWrapper = new QueryWrapper<>();
            likeWrapper.eq("comment_id", comment.getId()).eq("user_id", userId);
            int likeCount = commentLikeMapper.selectCount(likeWrapper).intValue();
            comment.setIsLiked(likeCount);
        }

        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        Page resultPage = new Page(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
        return resultPage;
    }

    @Override
    public Result<?> addComment(Comment comment) {
        commentMapper.insert(comment);
        return Result.success();
    }

    @Override
    public Result<?> deleteComment(Integer id) {
        Comment comment = commentMapper.selectById(id);
        Integer rootCommentId = comment.getRootCommentId();
        comment.setStatus(2);

        commentMapper.updateById(comment);

        if (rootCommentId == null) {
            List<Comment> replies = commentMapper.selectByRootCommentId(id);
            for (Comment reply : replies) {
                reply.setStatus(2);
                commentMapper.updateById(reply);
            }
        } else {
            List<Comment> replies = commentMapper.selectByToCommentId(id);
            for (Comment reply : replies) {
                reply.setStatus(2);
                commentMapper.updateById(reply);
            }
        }

        return Result.success();
    }

    @Override
    public Result<?> getCommentById(Integer id, Integer userId) {
        Comment comment = commentMapper.selectById(id);
        if(comment != null) {
            QueryWrapper<CommentLike> likeWrapper = new QueryWrapper<>();
            likeWrapper.eq("comment_id", id).eq("user_id", userId);
            int likeCount = commentLikeMapper.selectCount(likeWrapper).intValue();

            comment.setIsLiked(likeCount);
        }
        return Result.success(comment);
    }

    @Override
    public Result<?> likeComment(Integer id, Integer userId) {
        CommentLike commentLike = new CommentLike();
        commentLike.setCommentId(id);
        commentLike.setUserId(userId);
        commentLikeMapper.insert(commentLike);

        Comment comment = commentMapper.selectById(id);
        Integer number = comment.getLikeNumber();
        number++;
        comment.setLikeNumber(number);
        commentMapper.updateById(comment);
        return Result.success();
    }

    @Override
    public Result<?> dislikeComment(Integer id, Integer userId) {
        Comment comment = commentMapper.selectById(id);

        QueryWrapper<CommentLike> likeWrapper = new QueryWrapper<>();
        likeWrapper.eq("comment_id", id).eq("user_id", userId);
        commentLikeMapper.delete(likeWrapper);

        Integer number = comment.getLikeNumber();
        number--;
        comment.setLikeNumber(number);
        commentMapper.updateById(comment);
        return Result.success();
    }
}
